package com.hc.dfs.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hc.dfs.activity.R;
import com.hc.dfs.adapter.ViewPagerAdapter;
import com.hc.dfs.handle.ManageTab;
import com.hc.dfs.handle.ManageTabItem;
import com.hc.dfs.service.MainService;
import com.hc.dfs.task.Task;

/** 
 * @author 王洪胜  E-mail:  
 * @version 创建时间：2013-2-18 下午2:45:07
 * 
 * 类说明：公文签批栏目主Activity
 *
 */
public class ManageActivity extends BaseActivity{

	//获取数据成功
	public static final int GET_UNSIGNED_DOCUMENT_LIST_SUCCESS = 0;//待签
	public static final int GET_FOLLOW_DOCUMENT_LIST_SUCCESS = 1;//跟踪
	public static final int GET_TODO_DOCUMENT_LIST_SUCCESS = 2;//待办
	//顶部文字标签按钮
	private TextView textSigning_p, textFollow, textProcessing;
	//顶部标签按钮切换动画图标
	private ImageView cursor;
	// 动画图片偏移量
	private int offset = 0;
	// 当前页卡编号
	private int currIndex = 0;
	// 动画图片宽度
	private int bmpW;
	//内容页集合
	private ViewPager viewPager;
    //内容页要放的view合集
	private List<View> pageList = new ArrayList<View>();
	//内容页要放的单个view
	private View tab_view;
    //处理个内容页的类合集
	private List<ManageTab> signingTabLists = new ArrayList<ManageTab>();
    //内容页适配器
	private ViewPagerAdapter viewAdapter;
	//存储后台服务返回的数据
	private ArrayList<HashMap<String, Object>> listItem;
	//加载数据缓冲进度条
	private LinearLayout viewBar;
	//静态变量记录当前页码
	private static int page;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage_tab);
		//顶部viewPager标签按钮
		initTextButton();
		//初始化顶部文字颜色
		setTextColor(0);
		//顶部标签动画
		InitImageView();
		//添加待批公文实例，并获取view添加到的viewPager分页
		for(int i=0; i<3; i++){
			signingTab(i);
		}
		
		//初始化viewPager
		initViewPager();
	}

	//传递服务器返回的数据方法
	public ArrayList<HashMap<String, Object>> getListItem(){
		return listItem;
	}
	
	/**
	 * 顶部文字按钮操作
	 */
	public void initTextButton(){
		//根据ID获取控件
		textSigning_p = (TextView) findViewById(R.id.textSigning_p);
		textFollow = (TextView) findViewById(R.id.textFollow);
		textProcessing = (TextView) findViewById(R.id.textProcessing);
		//给文字按钮绑定监听器
		textSigning_p.setOnClickListener(this);
		textFollow.setOnClickListener(this);
		textProcessing.setOnClickListener(this);

	}
	
	/**
	 * 待批公文部分
	 * 获取待批公文类，从分页类中获取view，
	 * 实现view中的各种操作，
	 * 把view添加到viewPager的列表
	 */
	public void signingTab(int pageId){
		//添加实例到实例列表
		signingTabLists.add(new ManageTab(R.layout.manage_list, this));
		//获取实例中的view
		tab_view = signingTabLists.get(pageId).getView();
		//获取缓冲进度条
		viewBar = (LinearLayout) tab_view.findViewById(R.id.progress);
		//添加公文签批view到lists
		pageList.add(tab_view);
	}
	
	/**
	 * 初始化viewPager
	 */
	public void initViewPager(){
		//获取viewPager控件
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		//lists绑定到适配器
		viewAdapter = new ViewPagerAdapter(pageList);
		//给viewPager设定适配器
		viewPager.setAdapter(viewAdapter);
		//给viewPager绑定监听器
		viewPager.setOnPageChangeListener(new PageChangeListener());
	}
	
	/**
	 * 初始化动画
	 */
	private void InitImageView() {
		cursor = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.qianpi_top_redline)
				.getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		cursor.getLayoutParams().width=screenW / 3;
		offset = (screenW / 3 - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
	}
	
	/**
	 * pageChangeListener监听器实现类
	 * 
	 */
	class PageChangeListener implements OnPageChangeListener{
		
		int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
		int two = one * 2;// 页卡1 -> 页卡3 偏移量
		
		@Override
		public void onPageScrollStateChanged(int arg0) {
//			System.out.println("@"+arg0);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
//			System.out.println("@#"+arg0+"@"+arg1+"@"+arg2);
		}

		@Override
		public void onPageSelected(int arg0) {
//			System.out.println("@$$$"+arg0);
			//页面切换时，装载对应的数据
//			signingTabLists.get(arg0).setMyAdapter(arg0);
//			//页面切换时，绑定对应的监听器
//			signingTabLists.get(arg0).setListener(arg0);
			
			//显示数据读取进度条
			viewBar.setVisibility(View.VISIBLE);
			
			//传递当前页码到全局变量page
			page= arg0;
			
			//页面切换动画
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					//发送广播
//					sendBroadcast(new Intent("com.hc.dfs.displayButton"));
					animation = new TranslateAnimation(one, 0, 0, 0);
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(two, 0, 0, 0);
				}
				MainService.newTask(new Task(Task.GET_USIGNED_DOCMENT_LIST, null));
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, one, 0, 0);
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(two, one, 0, 0);
				}
				MainService.newTask(new Task(Task.GET_FOLLOW_DOCUMENT_LIST, null));
				break;
			case 2:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, two, 0, 0);
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(one, two, 0, 0);
				}
				MainService.newTask(new Task(Task.GET_TODO_DOCUMENT_LIST, null));
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(300);
			cursor.startAnimation(animation);
			//页面切换时，设置顶部文字颜色
			setTextColor(arg0);
		}
	}
	
	//设置文字颜色
	public void setTextColor(int a){
		TextView[] t = new TextView[]{textSigning_p, textFollow, textProcessing};
		for(int i=0; i<t.length; i++){
			TextPaint tp = t[i].getPaint();
			if(i==a){
				t[i].setTextColor(android.graphics.Color.RED);
				tp.setFakeBoldText(true);
			}else{
				t[i].setTextColor(android.graphics.Color.GRAY);
				tp.setFakeBoldText(false);
			}
		}
	}
	
	//长安条目弹出操作菜单处理
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		 //委托给SigningTabItem类处理
		new ManageTabItem(this, page).onContextItemSelected(item);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.textSigning_p:
			viewPager.setCurrentItem(0, true);
			break;
		case R.id.textFollow:
			viewPager.setCurrentItem(1, true);
			break;
		case R.id.textProcessing:
			viewPager.setCurrentItem(2, true);
			break;
		default:
			break;
		}
	}

	// 后台服务返回数据时，根据返回结果刷新UI
	@Override
	public void refresh(Object... param) {
		
		//接受数据
		listItem = (ArrayList<HashMap<String, Object>>) param[1];
		if(listItem!=null){
			//判断数据标志
			switch (((Integer)param[0]).intValue()) {
			case GET_UNSIGNED_DOCUMENT_LIST_SUCCESS:
				signingTabLists.get(0).setMyAdapter(0);
				signingTabLists.get(0).setListener(0);
//				setTextColor(0);
				//隐藏进度条
				viewBar.setVisibility(View.GONE);
				break;
			case GET_FOLLOW_DOCUMENT_LIST_SUCCESS:
				signingTabLists.get(1).setMyAdapter(1);
				signingTabLists.get(1).setListener(1);
//				setTextColor(1);
				//隐藏进度条
				viewBar.setVisibility(View.GONE);
				break;
			case GET_TODO_DOCUMENT_LIST_SUCCESS:
				signingTabLists.get(2).setMyAdapter(2);
				signingTabLists.get(2).setListener(2);
//				setTextColor(2);
				//隐藏进度条
				viewBar.setVisibility(View.GONE);
				break;
			default:
				break;
			}
			//隐藏进度条
			viewBar.setVisibility(View.GONE);
		}else{
			Toast.makeText(this, "数据加载失败，请稍后重试...", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * activity初始化方法，继承自baseActivity的抽象方法
	 * 创建activity时会先检查网络状态，网络正常，则执行此方法
	 */
	@Override
	public void init() {
		// 初始化时，如果数据列表为空，则添加数据请求任务
//		if(listItem==null){
			Task task = new Task(Task.GET_USIGNED_DOCMENT_LIST, null);
			MainService.newTask(task);
			System.out.println("在这里启动任务，比如请求网络数据");
//		}
	}

}
